package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.persistence.AccountDao;
import csu.web.mypetstore.persistence.impl.AccountDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class NewOrderFormServlet extends HttpServlet {
    private static final String NEW_ORDER_FORM = "/WEB-INF/jsp/order/newOrder.jsp";
    private static final String SIGNON_FORM = "/WEB-INF/jsp/account/signon.jsp";
    private static final String NULL_FORM = "/WEB-INF/jsp/common/cartnull.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Account loginAccount = (Account) session.getAttribute("loginAccount");
        AccountDao accountDao = new AccountDaoImpl();

        if (loginAccount == null) {
            req.getRequestDispatcher(SIGNON_FORM).forward(req, resp);
        }
        else {
            if(accountDao.selectShoppingcartByUsernameAndTimes(loginAccount)){

                req.getRequestDispatcher(NEW_ORDER_FORM).forward(req,resp);
            }else{
                req.getRequestDispatcher(NULL_FORM).forward(req, resp);

            }
        }
    }
}